﻿''' <summary>
''' 数据表操作基类
''' </summary>
Public MustInherit Class ClsBaseDbAccess
    '用户表 ： data/l_xxx.mdb库中 T_LedgerSetting表
    'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\自写系统\rjcwxt\Rjcwxt\data\l_xxx.mdb;Persist Security Info=True
    Private m_ConnectionString As String '数据库连接字符串
    Private m_AppTableName As String '在库中的表名 T_LedgerSetting

    Shared m_BaseDbAccess As ClsDbAccessMdb '保存数据库操作变量

    Public Sub New()

    End Sub
    Public Sub New(ByVal connString As String)
        '判断一下，value是否等于当下的m_connectionstring

        Dim currentConnectionString As String

        If m_BaseDbAccess IsNot Nothing Then
            '如果当下的连接串不等于给定的，则注销当下的数据库操作对象
            currentConnectionString = m_BaseDbAccess.ConnectionString
            If Not connString.Equals(currentConnectionString) Then
                m_BaseDbAccess = Nothing
            End If
        End If

        Me.ConnectionString = connString
    End Sub

    Public Sub New(ByVal connString As String, ByVal tableName As String)
        '判断一下，value是否等于当下的m_connectionstring

        Dim currentConnectionString As String

        If m_BaseDbAccess IsNot Nothing Then
            '如果当下的连接串不等于给定的，则注销当下的数据库操作对象
            currentConnectionString = m_BaseDbAccess.ConnectionString
            If Not connString.Equals(currentConnectionString) Then
                m_BaseDbAccess = Nothing
            End If
        End If

        Me.ConnectionString = connString
        Me.AppTableName = tableName

    End Sub


    Public Property ConnectionString As String
        Get
            Return m_ConnectionString
        End Get
        Set(value As String)
            m_ConnectionString = value
        End Set
    End Property

    Public Property AppTableName As String
        Get
            Return m_AppTableName
        End Get
        Set(value As String)
            m_AppTableName = value
        End Set
    End Property

    Public Function DeleteAllRecords() As Int32
        Try

            'Dim tmpRetList As List(Of T)
            Dim tmpRetInt As Integer
            Dim tmpSql As String
            '获得数据库连接
            Dim tmpDbAccess = GetConnection()

            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "delete * from [" & m_AppTableName & "] "

            '将一行数据转为类对象
            'tmpRetList = CType(ConvertRecordToClass(tmpDbAccess.ExecuteSqlReturnDataSet(tmpSql)), List(Of T))
            tmpRetInt = tmpDbAccess.ExecuteSqlNoDataReturn(tmpSql, New OleDb.OleDbParameter() {})
            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpRetInt

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsBaseDbAccess - DeletAllRecords"

            Return 0
        End Try

    End Function

    ''' <summary>
    ''' 泛型取全部记录
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <returns></returns>
    Public Function GetAllRecords(Of T)(Optional ByVal orderName As String = "", Optional ByVal asc As Boolean = True) As List(Of T)
        Try

            Dim tmpRetList As List(Of T)
            Dim tmpSql As String
            '获得数据库连接
            Dim tmpDbAccess = GetConnection()

            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "select * from [" & m_AppTableName & "] "

            If Not orderName.Equals("") Then
                tmpSql &= " order by [" & orderName & "]"

                tmpSql &= IIf(asc, " asc ", " desc ")
            End If

            '将一行数据转为类对象
            tmpRetList = CType(ConvertRecordToClass(tmpDbAccess.ExecuteSqlReturnDataSet(tmpSql)), List(Of T))
            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpRetList

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsBaseDbAccess - GetLedgerSetting"

            Return Nothing
        End Try
    End Function

    'Public MustOverride Function ConvertRecordToClass(Of T)(ByVal reader As OleDb.OleDbDataReader) As T
    Public MustOverride Function ConvertRecordToClass(ByVal reader As OleDb.OleDbDataReader) As Object


    Public Function GetConnection() As ClsDbAccessMdb

        If m_BaseDbAccess Is Nothing Then
            If m_ConnectionString IsNot Nothing Then
                m_BaseDbAccess = New ClsDbAccessMdb(m_ConnectionString)
                Return m_BaseDbAccess
            Else
                Throw New ArgumentNullException("使用数据库之前请先给连接字串赋值。")
            End If
        Else
            Return m_BaseDbAccess
        End If

    End Function
End Class
